/*
 * Copyright (C) 2024 HiHope Open Source Organization.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { createOrGet, Log } from '@ohos/common';
import { ShareManager, ConfirmFileInfo, Constants } from '@ohos/share';
import common from '@ohos.app.ability.common';
import { settings } from '@kit.BasicServicesKit';

const TAG = 'ConfirmDialogModel'

export class ConfirmDialogModel {
  context: common.UIExtensionContext;

  init(context: common.UIExtensionContext) {
    this.context = context;
  }

  parseConfirmFileInfo(fileInfoString: string): ConfirmFileInfo {
    let confirmFileInfo: ConfirmFileInfo = {
      type: 2, fileCount: [0], totalSize: 0
    }
    try {
      confirmFileInfo = JSON.parse(fileInfoString)
    } catch (error) {
      Log.error(TAG, `parseConfirmFileInfo failed: ${error}, ${JSON.stringify(error)}`)
    }
    return confirmFileInfo
  }

  refuseConnect() {
    ShareManager.refuseConnect()
  }

  confirmConnect() {
    ShareManager.confirmConnect()
  }

  saveConfirmFileInfo(fileInfo: ConfirmFileInfo) {
    Log.info(TAG, 'saveConfirmFileInfo in');
    settings.setValueSync(this.context, Constants.CONFIRM_FILE_INFO_SETTINGS_KEY, JSON.stringify(fileInfo));
  }
}

const sConfirmDialogModel: ConfirmDialogModel = createOrGet(ConfirmDialogModel, 'ConfirmDialogModel')

export default sConfirmDialogModel